package com.company.sendemo.basics.afJavaArray;

import java.util.Arrays;

public class JavaArray {


    //数组的初始化
    //静态初始化
    int one[] = new int[]{20, 21, 24};
    int two[] = {34, 32, 35}; //简化方式
    //动态初始化
    int[] three = new int[3];
    //错误写法:静态初始化不能指定元素个数
//    int four = new int[3]{23,21,32};
    //错误写法：动态初始化必须指定元素个数
//    int five[] = new int[];

    //数组 排序
    public static void main(String[] args) {
        int[] ccc = new int[]{2, 21, 242,-12};
        for (int i = 0; i < ccc.length - 1; i++) {
            for (int j = 0; j < ccc.length - 1 - i; j++) {
                if (ccc[j] > ccc[j + 1]) {
                    //变量交换
                    int t = ccc[j+1];
                    ccc[j + 1] = ccc[j];
                    ccc[j] = t;
                }
            }
        }
        //打印数组四种方法
        //1 循环打印数组
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < ccc.length; i++) {
            if (i>0){
                sb.append(",");
            }
            sb.append(ccc[i]);
        }
        System.out.println(sb.toString());

        //2 使用Arrays.toString()方法
        System.out.println(Arrays.toString(ccc));

        //3 使用JDK 8 的 java.util.Arrays.stream()
        Arrays.stream(ccc).forEach(System.out::println);

        //4 使用Arrays.deepToString()  对于deepToString，若为多维数组则会用同样的方法打印
        int[][] cccc = {{2,5,7}, {21}, {242,3},{-12,23}};
        Object[] cc = {2, 21, 242,-12};
        //4.1使用Arrays.deepToString()
        System.out.println(Arrays.deepToString(cc));  //int类型数组不行
        //4.2对于deepToString，若为多维数组则会用同样的方法打印
        System.out.println(Arrays.deepToString(cccc));
    }

}
